package org.dromara.tearoom.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.tearoom.domain.TerImgInfo;
import org.dromara.tearoom.domain.bo.TerImgInfoBo;
import org.dromara.tearoom.domain.vo.TerImgInfoVo;
import org.dromara.tearoom.mapper.TerImgInfoMapper;
import org.dromara.tearoom.service.ITerImgInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 贴图管理Service业务层处理
 *
 * @author xch
 * @date 2025-05-06
 */
@RequiredArgsConstructor
@Service
public class TerImgInfoServiceImpl implements ITerImgInfoService {

    private final TerImgInfoMapper baseMapper;

    /**
     * 查询贴图管理
     *
     * @param id 主键
     * @return 贴图管理
     */
    @Override
    public TerImgInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询贴图管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 贴图管理分页列表
     */
    @Override
    public TableDataInfo<TerImgInfoVo> queryPageList(TerImgInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerImgInfo> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(TerImgInfo::getCreateTime);
        Page<TerImgInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的贴图管理列表
     *
     * @param bo 查询条件
     * @return 贴图管理列表
     */
    @Override
    public List<TerImgInfoVo> queryList(TerImgInfoBo bo) {
        LambdaQueryWrapper<TerImgInfo> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(TerImgInfo::getOrderNo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerImgInfo> buildQueryWrapper(TerImgInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerImgInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getModule() != null, TerImgInfo::getModule, bo.getModule());
        lqw.eq(bo.getStatus() != null, TerImgInfo::getStatus, bo.getStatus());
        lqw.eq(bo.getOrderNo() != null, TerImgInfo::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getImgType() != null, TerImgInfo::getImgType, bo.getImgType());
        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), TerImgInfo::getImgUrl, bo.getImgUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getAssoId()), TerImgInfo::getAssoId, bo.getAssoId());
        lqw.le(ObjectUtil.isNotEmpty(bo.getNowDate()),TerImgInfo::getStartTime, bo.getNowDate());
        lqw.gt(ObjectUtil.isNotEmpty(bo.getNowDate()),TerImgInfo::getEndTime, bo.getNowDate());
        return lqw;
    }

    /**
     * 新增贴图管理
     *
     * @param bo 贴图管理
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TerImgInfoBo bo) {
        TerImgInfo add = MapstructUtils.convert(bo, TerImgInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改贴图管理
     *
     * @param bo 贴图管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerImgInfoBo bo) {
        TerImgInfo update = MapstructUtils.convert(bo, TerImgInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerImgInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除贴图管理信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 修改状态
     *
     * @param id     用户ID
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updateStatus(Long id, Integer status) {
        TerImgInfoVo imgInfoVo=baseMapper.selectVoById(id);
        if(ObjectUtil.isEmpty(imgInfoVo)){
            throw new ServiceException("数据不存在");
        }
        return baseMapper.update(null,
                new LambdaUpdateWrapper<TerImgInfo>()
                        .set(TerImgInfo::getStatus, status)
                        .eq(TerImgInfo::getId, id));
    }
}
